package com.primeton.viewtest;

import java.util.Date;

import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EPStatement;
import com.espertech.esper.client.EventBean;
import com.espertech.esper.client.StatementAwareUpdateListener;

public class FickUpdateListener implements StatementAwareUpdateListener {
	@Override
	public void update(EventBean[] newData, EventBean[] oldData, EPStatement epStatement,
			EPServiceProvider epServiceProvider) {
		System.out.println("FicUpdateListener (1) update");
		for (int i = 0; i < newData.length; i++) {
			System.out.println("Statement " + String.format("%s", epStatement.getName()) + " produced: "
					+ getProperties(newData[i]));
		}
	}

	private String getProperties(EventBean theEvent) {
		StringBuilder buf = new StringBuilder();

		for (String name : theEvent.getEventType().getPropertyNames()) {
			Object value = theEvent.get(name);
			buf.append(name);
			buf.append("=");

			if (name.contains("timestamp")) {
				buf.append(new Date((Long) value));
			} else {
				buf.append(value);
			}
			buf.append(" ");
		}
		return buf.toString();
	}
}
